package com.example.excel.db;

import android.util.Log;

import com.example.excel.bean.InventoryBean;
import com.example.excel.bean.InventoryBeanDao;
import com.example.excel.config.Constants;

import org.greenrobot.greendao.query.QueryBuilder;

import java.util.List;

public class InventoryController {
    private static final String TAG = InventoryController.class.getSimpleName();
    private final DbController mManager;
    private static InventoryController mController;

    public static InventoryController getInstance() {
        if (mController == null) {
            synchronized (InventoryController.class) {
                if (mController == null) {
                    mController = new InventoryController();
                }
            }
        }
        return mController;
    }

    private InventoryController() {
        mManager = DbController.getInstance();
    }

    /**
     * 记录的插入，如果表未创建，先创建InventoryBean表
     */
    public boolean insert(InventoryBean bean) {
        boolean flag = false;
        flag = mManager.getDaoSession().getInventoryBeanDao().insert(bean) != -1;
        return flag;
    }

    /**
     * 插入多条数据，在子线程操作
     */
    public boolean insertInTx(final List<InventoryBean> beanList) {
        boolean flag = false;
        try {
            mManager.getDaoSession().runInTx(new Runnable() {
                @Override
                public void run() {
                    for (InventoryBean bean : beanList) {
                        mManager.getDaoSession().insertOrReplace(bean);
                    }
                }
            });
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * 修改一条数据
     */
    public boolean update(InventoryBean bean) {
        boolean flag = false;
        try {
            mManager.getDaoSession().update(bean);
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }


    /**
     * 删除所有记录
     */
    public boolean deleteAll() {
        boolean flag = false;
        try {
            //按照id删除
            mManager.getDaoSession().deleteAll(InventoryBean.class);
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * 查询所有记录
     */
    public List<InventoryBean> queryAllInventory() {
        return mManager.getDaoSession().loadAll(InventoryBean.class);
    }

    public List<InventoryBean> queryByCode(String faCode) {
        QueryBuilder<InventoryBean> queryBuilder = mManager.getDaoSession().queryBuilder(InventoryBean.class);
        return queryBuilder.where(InventoryBeanDao.Properties.FaCode.eq(faCode)).list();
    }

    /**
     * 获取已盘点数据,未上传的
     */
    public List<InventoryBean> queryInventory(int offset) {
        QueryBuilder<InventoryBean> queryBuilder = mManager.getDaoSession().queryBuilder(InventoryBean.class);
        queryBuilder.where(InventoryBeanDao.Properties.FaFlag.eq(1));
        queryBuilder.offset(offset * Constants.PAGE_SIZE).limit(Constants.PAGE_SIZE);
        return queryBuilder.list();
    }

    /**
     * 获取未盘点数据
     */
    public List<InventoryBean> queryNoInventoryData() {
        QueryBuilder<InventoryBean> queryBuilder = mManager.getDaoSession().queryBuilder(InventoryBean.class);
        return queryBuilder.where(InventoryBeanDao.Properties.FaFlag.eq(0)).offset(0).limit(10).list();
    }

    /**
     * 盘点数据
     *
     * @param index  0：
     * @param offset
     * @return
     */
    public List<InventoryBean> queryInventory(int index, int offset) {
        int i = 0;
        if (index == 0) {
            i = 1; // 已盘
        }
        QueryBuilder<InventoryBean> queryBuilder = mManager.getDaoSession().queryBuilder(InventoryBean.class);
        return queryBuilder
                .where(InventoryBeanDao.Properties.FaFlag.eq(i))
                .offset(offset * Constants.PAGE_SIZE)
                .limit(Constants.PAGE_SIZE)
                .orderAsc(InventoryBeanDao.Properties.FaCode)
                .list();
    }

    public List<InventoryBean> queryAll(int index) {
        int i = 0;
        if (index == 0) {
            i = 1; // 已盘
        }
        QueryBuilder<InventoryBean> queryBuilder = mManager.getDaoSession().queryBuilder(InventoryBean.class);
        return queryBuilder.where(InventoryBeanDao.Properties.FaFlag.eq(i)).list();
    }
}
